package com.backend.demo.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.Menu;
import com.backend.demo.service.IMenuService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 菜单管理
 * @author Administrator
 *
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class MenuAction extends ActionSupport implements RequestAware,ModelDriven<Menu>,SessionAware {

	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> request;
	
	private Map<String,Object> session;
	
	private Menu menu;
	
	@Resource(name="menuService")
	private IMenuService menuService;
	
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
	public Menu getMenu() {
		return menu;
	}
	
	@Action(value="getAllMenus",results={@Result(name=SUCCESS,location="/jspView/menus/menuList.jsp")})
	public String getAllMenus(){
		List<Menu> menus = menuService.getAllMenu();
		if(menus != null && menus.size() > 0){
			request.put("menus", menus);
			return SUCCESS;
		}
		return ERROR;
	}
	
	/*@Action(value="getAllMenus",results={@Result(name=SUCCESS,location="/jspView/menus/menuList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllMenusByPage(){
		PageBean<Menu> pageResults = menuService.getAllMenusByPage();
		request.put("pageResult", pageResults);
		request.put("menus", pageResults.getResults());
		request.put("totalCount", pageResults.getTotalCount());
		request.put("currentPage", pageResults.getCurrentPage());
		request.put("pageCount", pageResults.getPageCount());
		return SUCCESS;
	}*/
	
	@Action(value="redirectMenu",results={@Result(name=SUCCESS,location="/jspView/menus/addMenu.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectMenu(){
		return SUCCESS;
	}
	
	@Action(value="addMenu",results={@Result(name=SUCCESS,location="/jspView/getAllMenus",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addMenu(){
		int num = menuService.addMenu(menu);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="getMenusByCondition",results={@Result(name=SUCCESS,location="/jspView/menus/menuList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getMenusByCondition(){
		List<Menu> menus = menuService.getMenuByCondition(menu);
		request.put("menus", menus);
		return SUCCESS;
	}
	
	@Action(value="getMenuById",results={@Result(name=SUCCESS,location="/jspView/menus/editMenu.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getMenuById(){
		Menu menuItem = menuService.getMenuById(menu.getMenuID());
		if(menuItem != null){
			request.put("menu", menuItem);
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editMenu",results={@Result(name=SUCCESS,location="/jspView/getAllMenus",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editMenu(){
		int num = menuService.updateMenu(menu);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deleteMenu",results={@Result(name=SUCCESS,location="/jspView/getAllMenus",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deleteMenu(){
		int num = menuService.deleteMenu(menu.getMenuID());
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}

	@Override
	public Menu getModel() {
		if(menu == null){
			menu = new Menu();
		}
		return menu;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
